package com.szboanda.mpgz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.szboanda.mpgz.service.Mapper.MpgzDao;
import com.szboanda.mpgz.service.MggzService;
import com.szboanda.mpgz.utill.HttpClientUtilSsl;
import com.szboanda.mpgz.utill.SM3Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @auther 大雄
 * @create 2024-10-11 10:01:55
 */
@Service
public class MggzServiceImpl implements MggzService {
    @Autowired
    private MpgzDao mpgzDao;

    @Value("${mpgzUrl}")
    private String url;

    private String token;

    private String sign;


    @Override
    public void mpgz() {
        this.getToken();
        List<Map<String, Object>> listUser = mpgzDao.listAll();
        for (Map<String, Object> user : listUser) {
            String phone = (String) user.get("PHONE");
            String openid = (String) user.get("OPENID");
            if (StringUtils.isNotEmpty(phone)) {
                JSONObject jsonObject = this.encOrig(Base64.getEncoder().encodeToString(phone.getBytes()));
                String dataCipher = jsonObject.getJSONObject("data").getString("dataCipher");
                String keyCipher = jsonObject.getJSONObject("data").getString("keyCipher");
                mpgzDao.updataUserBySession_key(openid, dataCipher, keyCipher);
            }
        }
//        JSONObject jsonObject1 = this.decOrig(keyCipher, dataCipher);
//        String orig = jsonObject1.getJSONObject("data").getString("orig");
//        byte[] decode = Base64.getDecoder().decode(orig);
//        String dec = new String(decode);
//        System.out.println(dec);


//        System.out.println(list);
    }

    //加密原文
    private JSONObject encOrig(String orig) {
        String jmurl = url + "/cssp-server-sym/symEnc/symEncrypt";

        Map<String, Object> jmMap = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        Map<String, String> content = new HashMap<>();

        header.put("token", token);
        header.put("sign", sign);

        content.put("orig", orig);
        content.put("requestId", "08310001");
        content.put("tenantResourceCode", "symmetry10");

        jmMap.put("header", header);
        jmMap.put("content", content);

        String jmJson = HttpClientUtilSsl.doPostOfJson(jmurl, JSON.toJSONString(jmMap));
        JSONObject jmObj = JSON.parseObject(jmJson);
        if (jmObj.getString("code").equals("401")) {
            this.getToken();
            jmObj = encOrig(orig);
        }
        return jmObj;
    }

    //解密原文
    private JSONObject decOrig(String keyCipher, String dataCipher) {
        String jmurl = url + "/cssp-server-sym/symDec/symDecrypt";

        Map<String, Object> jmMap = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        Map<String, String> content = new HashMap<>();

        header.put("token", token);
        header.put("sign", sign);

        content.put("keyCipher", keyCipher);
        content.put("dataCipher", dataCipher);
        content.put("requestId", "08310001");
        content.put("tenantResourceCode", "symmetry10");

        jmMap.put("header", header);
        jmMap.put("content", content);

        String jmJson = HttpClientUtilSsl.doPostOfJson(jmurl, JSON.toJSONString(jmMap));
        JSONObject jmObj = JSON.parseObject(jmJson);
        if (jmObj.getString("code").equals("401")) {
            this.getToken();
            jmObj = decOrig(keyCipher, dataCipher);
        }
        return jmObj;
    }

    private void getToken() {
        //对微信小程序用户的手机号继续加密储存
        String yhbs = "2KhfhBvu";
        String sqm = "2UKzlzNF";
        String yybm = "08310001";
        String sjc = System.currentTimeMillis() + "";
        String uuid = UUID.randomUUID().toString();

        String a = yhbs + yybm + sjc + uuid;
        String sign = "";
        try {
            sign = SM3Utils.hmac(sqm, a);
            System.out.println("sign" + sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("用户标识" + yhbs);
        System.out.println("用户授权码" + sqm);
        System.out.println("应用编码" + yybm);
        System.out.println("时间戳" + sjc);
        System.out.println("uuid" + uuid);

        String urltoken = url + "/cssp-uaa/auth/getToken";
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("sign", sign);
        requestParams.put("tenantCode", yhbs);
        requestParams.put("appCode", yybm);
        requestParams.put("version", "1.0");
        requestParams.put("ctime", sjc);
        requestParams.put("random", uuid);

        //获取token
        String tokenJson = HttpClientUtilSsl.doPost(urltoken, requestParams, null);
        JSONObject tokenObj = JSON.parseObject(tokenJson);

        String access_token = tokenObj.getJSONObject("data").getString("access_token");

        token = access_token;
        this.sign = sign;
    }
}
